package practica3as;



import entities.Composite;
import entities.Drawable;
import java.awt.Graphics;
import java.util.logging.Level;
import java.util.logging.Logger;

public class Renderer extends Thread {

    private Composite scene;
    private Principal applet;
    private boolean stop;
    private Graphics offImage;

    public Renderer(Drawable scene, Principal applet) {
        this.scene = (Composite) scene;
        this.applet = applet;
        offImage = applet.getjDoubleBufferedPanel().getImage().getGraphics();
    }

    @Override
    public void run() {
        while (!stop) {
            offImage.clearRect(0, 0, 500, 400);
            synchronized (scene) {
                scene.render(offImage);
            }
            applet.getjDoubleBufferedPanel().repaint();
            try {
                Thread.sleep(5);
            } catch (InterruptedException ex) {
                Logger.getLogger(Renderer.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
    }

    public void setStop(boolean stop) {
        this.stop = stop;
    }

    void setScene(Composite scene) {
        synchronized (scene) {
            this.scene = scene;
        }
    }
}
